package com.whutosa.template.server.controller;

import com.whutosa.template.server.model.Person;
import com.whutosa.template.server.model.VO;
import com.whutosa.template.server.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by 19110 on 2016/9/11.
 */
@Api
@RestController
public class TemplateController {
    private static final Logger log = LogManager.getRootLogger();

    /**
     * 注入
     */
    @Autowired
    private PersonService personService;

    @ApiOperation(value = "获取所有人", response = Page.class)
    @GetMapping("getAllPeople")
    public Object getAllPeople(
            @ApiParam("页码") @RequestParam(name = "page", required = false) Integer page,
            @ApiParam("每页数量") @RequestParam(name = "size", required = false) Integer size
    ) {
        log.debug("获取所有Person");
        if (page == null || size == null) {
            page = 0;
            size = Integer.MAX_VALUE;
        }
        Page<Person> allPeople = personService.getAllPeople(page, size);
        return allPeople;
    }


    @ApiOperation("根据姓名查询")
    @GetMapping("findPeopleByName")
    public VO<Page<Person>> findPeopleByName(@RequestParam(name = "name") String name,
                                             @RequestParam(name = "page") int page,
                                             @RequestParam(name = "size") int size) {
        return new VO<>(personService.findByNameLike(name, page, size));
    }


    @ApiOperation("根据条件查询")
    @GetMapping("query")
    public Object query(
            Person condition,
            @RequestParam(name = "page") int page,
            @RequestParam(name = "size") int size
    ) {
        return personService.query(condition, page, size);
    }

    /**
     * 文件上传测试
     *
     * @param request
     * @param txtFile
     * @return
     * @throws IOException
     */
    @PostMapping(value = "uploadTest", produces = "text/plain;charset=UTF-8")
    public Object uploadTxtFile(
            HttpServletRequest request,
            @RequestParam(name = "txtFile") MultipartFile txtFile) throws IOException {
        InputStream inputStream = txtFile.getInputStream();
        String txt = IOUtils.toString(inputStream, request.getCharacterEncoding());
        return txt;
    }

    @ApiOperation("添加Attribute测试")
    @RequestMapping(value = "addattrtest", method = RequestMethod.POST)
    public Person test(@ApiParam(hidden = true) @RequestAttribute(value = "person", required = false) Person person,
                       @ApiParam(hidden = false) @RequestParam("token") String token
    ) {
        return person;
    }

}
